﻿using System;
using System.Windows;
using ARR.UI.ViewModels;
using ARR.UI.Views;

namespace ARR.UI.Helpers
{
    static class WindowManager
    {        
        public static void CloseAllViews<T>()
        {
            CloseAllViews(typeof(T));
        }

        public static void CloseView(ViewModelBase viewModel, bool? dialogResult = null)
        {
            foreach (Window window in Application.Current.Windows)
            {
                if (window.DataContext == viewModel)
                {
                    if (dialogResult.HasValue)
                        window.DialogResult = dialogResult;

                    window.DataContext = null;
                    window.Close();
                }
            }
        }                   

        private static void CloseAllViews(Type type)
        {
            foreach (Window window in Application.Current.Windows)
            {
                if (window.GetType() == type)
                {
                    window.DataContext = null;
                    window.Close();
                }
            }
        }
    }
}
